package cn.edu.leetcode.买卖股票的最佳时机;

public class 买卖股票的最佳时机2 {
    public static int maxProfit(int[] prices) {
        boolean mark = false;  // 标记股票卖入否
        int profit = 0;  //利润
        int price = 0;  //买入价格
        for (int i = 0; i < prices.length; i++) {
            if (mark) {
                if (i + 1 >= prices.length || prices[i] > prices[i + 1]) {
                    mark = false;
                    profit += prices[i] - price;
                }
            } else {
                if (i + 1 < prices.length && prices[i] < prices[i + 1]) {
                    mark = true;
                    price = prices[i];
                }
            }
        }
        return profit;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }
}
